import Vue from 'vue'
import VueRouter from 'vue-router'

import Reg from '@/views/Reg.vue'
import Login from '@/views/Login.vue'
import Main from '@/views/Main.vue'
import Home from '@/views/Muens/Home.vue'
import UserInfo from '@/views/Muens/User/infor.vue'
import UserAvatar from '@/views/Muens/User/Avatar.vue'
import UserPwd from '@/views/Muens/User/Pwd.vue'

import store from '@/store'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    // { path: '/', redirect: Login },
    { path: '/reg', component: Reg },
    { path: '/login', component: Login },
    {
      path: '/',
      component: Main,
      redirect: '/home',
      children: [
        { path: 'home', component: Home },
        { path: 'user-info', component: UserInfo },
        { path: 'user-avatar', component: UserAvatar },
        { path: 'user-pwd', component: UserPwd }
      ]
    }
  ]
})

router.beforeEach((to, form, next) => {
  if (to.path === '/login' || to.path === '/reg') {
    next()
  } else {
    const token = store.state.user.token
    if (token) { next() } else { next('/login') }
  }
})

export default router
